<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');

need_login();
//order  commission_id  commission_money
$condition = array( 'commission_id' => $login_user_id ,'rstate'=>'normal');


$today_time = strtotime(date('Y-m-d'));
$today_condition = array('commission_id' => $login_user_id ,'create_time>'.$today_time,'rstate'=>'normal');

$today_count = Table::Count('order', $today_condition);

$yesday_time = $today_time - 86400;
$yesday_condition = array('commission_id' => $login_user_id ,'create_time>'.$yesday_time.' and create_time<'.$today_time,'rstate'=>'normal');
$yesday_count = Table::Count('order', $yesday_condition);

$cur_foot = 'mysetting';

if(isset($INI['system']['secsite']) && !empty($INI['system']['secsite']))
	$cur_url = $INI['system']['wwwprefix'].$INI['system']['secsite'].'account/mytuanorders.php';
else 	
	$cur_url = $INI['system']['wwwprefix'].'account/mytuanorders.php';

$count = Table::Count('order', $condition);

list($pagesize, $offset, $pagestring) = pagestring($count, 10);

$orders = DB::LimitQuery('order', array(
	'condition' => $condition,
	'order' => 'ORDER BY  id desc',
	'size' => $pagesize,
	'offset' => $offset,
));

foreach($orders as &$order)
{
	$order['pin_state'] = check_ordertuan_success($order['id']);
	//$order[]
}
//user_id


$team_ids = Utility::GetColumn($orders, 'team_id');
$teams = Table::Fetch('team', $team_ids);
foreach($teams AS $tid=>$one){
	team_state($one);
	$teams[$tid] = $one;
}

$user_ids = Utility::GetColumn($orders, 'user_id');
$users = Table::Fetch('user', $user_ids);
foreach($users AS $tid=>$one){
	$users[$tid] = $one;
}



if(isset($_GET['page'])  && intval($_GET['page']) > 1)
{
	$s_html = '';
	$result = array('ret'=>1,'offset'=>$offset);
	if(!empty($orders)){
		$result['ret'] = 0;
	}
	foreach($orders as $index =>$one)
	{	
			$s_html .= '<li class="member-browse-li">';
			$s_html .= '	<div class="row member-browse-summey">';
			$s_html .= '		<a class="member-browse-summey-info" href="#">       ';          
			$s_html .= '			<div class="member-browse-nt">                 ';          
			$s_html .= '				<span class="member-browse-name" style="font-weight:bold;">';
			$s_html .= '					订单：'.$one['id'];
			$s_html .= '					<span class="pro-price">【';
								
			if($one['state'] == 'pay')
			{
				if($one['pin_state'] == 1)
				{
					$s_html .= '已完成';
				} else {
					$s_html .= '拼团中';
				}
			}else {
				$s_html .= '待支付';
			}
								
			$s_html .= '】</span>';
			$s_html .= '				</span>';
			$s_html .= '			</div>';
			$s_html .= '		</a>   ';                
			$s_html .= '	</div>';
			$s_html .= '	<div class="member-browser-pro-list">';
			$s_html .= '		<a class="member-browser-pro-a" href="#"><span class="pro-img">';
			$s_html .= '			<img src="'.team_image($teams[$one['team_id']]['image']).'"></span>';
			$s_html .= '			<p class="pro-info"><span class="pro-name" style="color:#999;">'.$teams[$one['team_id']]['title'].'</span>';
			$s_html .= '			<span class="pro-price">预估佣金：<strong>+'.$one['commission_money'].'</strong></span>';
			$s_html .= '			<span class="pro-price">&nbsp;&nbsp;&nbsp;&nbsp;数量：<strong>'.$one['quantity'].'</strong></span></p>';
			$s_html .= '		</a>';
			$s_html .= '	</div>';
			$s_html .= '	<div class="row member-browse-summey">';
			$s_html .= '		<a class="member-browse-summey-info" href="#">      ';           
			$s_html .= '			<div class="member-browse-nt"> ';                          
			$s_html .= '				<span class="member-browse-name" style="color:#999;">购买人：'.$users[$one['user_id']]['username'].'&nbsp;&nbsp;&nbsp;&nbsp;金额：'.$one['origin'];
			$s_html .= '				<br/>日期：'.date('Y-m-d',$one['create_time']).'</span>';
			$s_html .= '			</div>';
			$s_html .= '		</a>   ';                
			$s_html .= '	</div>';
			$s_html .= '</li>';
	}
	
	$result['html'] = $s_html;
	echo json_encode($result);
	die();
}


$pagetitle = '推广订单';
include template('account_mytuanorders');
